package com.xunji.xunji.module.account.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.huanxiao.base.base.BaseActivity;
import com.huanxiao.base.net.RespResult;
import com.huanxiao.base.util.AlertDialogUtil;
import com.huanxiao.base.view.ClearEditText;
import com.huanxiao.base.view.CountDownButton;
import com.huanxiao.base.view.DesignToolbar;
import com.huanxiao.util.StringHelper;
import com.xunji.xunji.R;
import com.xunji.xunji.module.account.controller.UserAccount;

import rx.Subscriber;

/**
 * 登录
 * Created by paul on 16/10/18.
 */

public class RegisterActivity extends BaseActivity {

    private ClearEditText mCetValidate;
    private ClearEditText mCetPhone;
    private EditText mCetPassword;
    private CountDownButton mCdbValidate;
    private Button mBtnLogin;
    private DesignToolbar mToolbar;

    private TextWatcher mTextWatcher;
    private TextWatcher mTextWatcherValidate;
    private TextWatcher mTextWatcherPassword;


    public static void start(Context context) {
        Intent starter = new Intent(context, RegisterActivity.class);
        context.startActivity(starter);
    }


    @Override
    protected void onDestroy() {
        if (mTextWatcher != null) {
            mCetValidate.removeTextChangedListener(mTextWatcherValidate);
            mCetPhone.removeTextChangedListener(mTextWatcher);
            mCetPassword.removeTextChangedListener(mTextWatcherPassword);
        }
        super.onDestroy();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    public void initBundleExtras(Bundle extras) {

    }

    @Override
    protected void initView() {
        mCetValidate = (ClearEditText) findViewById(R.id.cet_validate);
        mCdbValidate = (CountDownButton) findViewById(R.id.cdb_validate);

        mBtnLogin = (Button) findViewById(R.id.btn_next_step);
        mCetPhone = (ClearEditText) findViewById(R.id.cet_phone);
        mCetPassword = fvById(R.id.cet_password);
        mToolbar = (DesignToolbar) findViewById(R.id.toolbar);
    }

    @Override
    protected void initListener() {
        mTextWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                checkPhone();
            }
        };

        mTextWatcherValidate = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                checkValidate();
            }
        };
        mTextWatcherPassword = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                checkValidate();
            }
        };

        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });


        mCetValidate.addTextChangedListener(mTextWatcherValidate);
        mCetPhone.addTextChangedListener(mTextWatcher);
        mCetPassword.addTextChangedListener(mTextWatcherPassword);

        mCdbValidate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendSmsCode(mCetPhone.getText().toString(),null);


            }
        });

        mToolbar.setOnRightMenuClickListener(new DesignToolbar.OnRightMenuClickListener() {
            @Override
            public void onRightClick() {
                finish();
            }
        });

    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    @Override
    protected boolean isBindEventBus() {
        return false;
    }


    private void checkPhone() {
        if (StringHelper.is11Number(mCetPhone.getText().toString())) {
            mCdbValidate.setEnabled(true);
        } else {
            mCdbValidate.setEnabled(false);
        }
    }

    private void checkValidate() {
        if (StringHelper.is11Number(mCetPhone.getText().toString())) {
            if (!isEmpty(mCetValidate) && mCetPassword.getText().toString().length() >= 6) {
                mBtnLogin.setEnabled(true);
            } else {
                mBtnLogin.setEnabled(false);
            }
        } else {
            mBtnLogin.setEnabled(false);
        }
    }

    @Override
    protected int getStatusBarColor() {
        return R.color.white;
    }


    private void sendSmsCode(String phone,String verify) {
        showLoading("发送中...");
        UserAccount.getInstance()
                .sendSmsCode(phone,verify, new Subscriber<RespResult>() {
                    @Override
                    public void onCompleted() {
                        hideLoading();

                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoading();
                        e.printStackTrace();

                    }

                    @Override
                    public void onNext(RespResult respResult) {
                        hideLoading();
                        mCdbValidate.startTimer();
                    }
                });
    }

    private void register() {
        showLoading("正在登陆...");
        UserAccount.getInstance()
                .register(mCetPhone.getText().toString(), mCetValidate.getText().toString(),
                        mCetPassword.getText().toString(), new Subscriber<RespResult>() {
                            @Override
                            public void onCompleted() {
                                hideLoading();
                            }

                            @Override
                            public void onError(Throwable e) {
                                hideLoading();
                            }

                            @Override
                            public void onNext(RespResult respResult) {
                                hideLoading();
                                finish();
                                LoginActivity.start(RegisterActivity.this);

                            }
                        });
    }
}
